package questions;

import java.util.ArrayList;

public class QuestionPicker {
	private int questionsCount = 0;
	private int questionsCorrect = 0;
	private int questionsFalse = 0;
	private long startTime;
	private long endTime;
	private Question currentQuestion;
	private ArrayList<Question> usedQuestions;
	private ArrayList<Question> availableQuestions;
	
	public QuestionPicker(String file) {
		this.usedQuestions = new ArrayList<Question>();
		this.availableQuestions = new QuestionLoader(file).getQuestions();
	}
	
	public Question getNewQuestion() {
		if (availableQuestions.equals(null) || availableQuestions.size() == 0) {
			endTime = System.nanoTime();
			return null;
		}
		if (currentQuestion != null) 
			usedQuestions.add(currentQuestion);
		incQuestionsCount();
		currentQuestion = availableQuestions.get(0);
		availableQuestions.remove(0);
		return currentQuestion;
	}

	public int getQuestionsCount() {
		return questionsCount;
	}
	
	public int incQuestionsCount() {
		if (questionsCount == 0) startTime = System.nanoTime();
		return ++questionsCount;
	}

	public int getQuestionsCorrect() {
		return questionsCorrect;
	}
	
	public int incQuestionsCorrect() {
		return ++questionsCorrect;
	}

	public int getQuestionsFalse() {
		return questionsFalse;
	}
	
	public int incQuestionsFalse() {
		return ++questionsFalse;
	}

	public long getStartTime() {
		return startTime;
	}

	public long getEndTime() {
		return endTime;
	}
	
	public String answer(String answer) {
		this.currentQuestion.setGivenAnswer(answer);
		if (currentQuestion.isCorrect())
			incQuestionsCorrect();
		else
			incQuestionsFalse();
		return currentQuestion.getAwaitedAnswer();
	}

	public int getRemainingQuestionsNumber() {
		return this.availableQuestions.size();
	}

	public long getElapsedTime() {
		return getEndTime()-getStartTime();
	}
}
